package handler

import (
	"company/api/requset"
	"company/internal/dao"
	"company/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddUser(c *gin.Context) {
	//接收参数
	var req requset.AddUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	var user model.User
	dao.GetOneByFields(&model.User{Mobile: req.Mobile}, &user)
	if user.Id > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "员工已存在",
			"data": nil,
		})
		return
	}

	result := dao.Create(&model.User{
		Name:      req.Name,
		Mobile:    req.Mobile,
		Password:  req.Password,
		Sex:       req.Sex,
		Email:     req.Email,
		Address:   req.Address,
		Education: req.Education,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "添加员工失败",
			"data": false,
		})
		return
	}

	//返回
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加员工成功",
		"data": true,
	})

}

// 获取所有在线人员
func GetOnlineUser(c *gin.Context) {
	var result []model.User

	for userId, _ := range OnlineUser {
		var user model.User
		dao.GetOneById(userId, &user)
		if user.Id == 0 {
			continue
		}
		user.Password = ""
		result = append(result, user)
	}

	//返回
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "获取成功",
		"data": result,
	})

}
